import {
  mongoDB,
  relation_mongoDB,
} from '../../../../../conect_mongoDB_class_laugh_node';
import {
  relation_db_prefix,
  relation_describe_db_name,
} from '../DataDefine/sourceData';
import { describe_inv_or_rev } from '../Types';

export function get_db_relation_cur_meta_id(cur_meta_id: string) {
  return relation_mongoDB.collection(`${relation_db_prefix}_${cur_meta_id}`);
}

/**
 * 获取 关系 描述符 db
 * @returns
 */
export function get_db_relation_describe() {
  return mongoDB.collection(relation_describe_db_name);
}

/**
 * 获取 当前 描述 对应 反转描述
 * @param cur_describe
 * @returns
 */
export async function get_relation_describe_rev(
  cur_describe: string
): Promise<string> {
  let db_relation_describe = get_db_relation_describe();

  let inv_info = await db_relation_describe
    .find({ inv: cur_describe })
    .toArray();

  if (inv_info.length == 1) {
    return inv_info[0].rev;
  }

  let rev_info = await db_relation_describe
    .find({ rev: cur_describe })
    .toArray();

  if (rev_info.length == 1) {
    return rev_info[0].inv;
  }

  return cur_describe;
}

/**
 * 获取 当前 描述 上行 或 下行 描述符
 * @param cur_describe inv 下行 ||  rev 上行
 * @returns
 */
export async function get_relation_describe_inv_or_rev(
  inv_or_rev: describe_inv_or_rev
): Promise<string[]> {
  let ret = [];
  let db_relation_describe = get_db_relation_describe();

  let inv_info_arr = await db_relation_describe.find().toArray();

  if (inv_info_arr.length > 0) {
    for (const inv_info of inv_info_arr) {
      ret.push(inv_info[inv_or_rev]);
    }
  }

  return ret;
}
